package com.hfjyz.system.login.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.bean.MenuBean;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.entity.CodeEntity;
import com.hfjyz.base.entity.MenuEntity;
import com.hfjyz.base.entity.UserEntity;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.base.util.CodeUtil;
import com.hfjyz.base.util.MenuUtil;
import com.hfjyz.system.login.service.LoginService;
import com.hfjyz.system.user.bean.UserBean;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -1563648233093852087L;

    private String userId = null;
    private String password = null;
    
    private Map<String, Object> map = new HashMap<String, Object>();

    private LoginService service = null;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public LoginService getService() {
        return service;
    }

    public void setService(LoginService service) {
        this.service = service;
    }

    public String login() {
        try {
            UserEntity user = service.login(userId, password);
            UserBean bean = new UserBean(user);
            List<MenuEntity> menuList = service.getMenuList(user);
            List<MenuEntity> allMenu = service.getAllMenu();
            List<MenuBean> menuInfoList = MenuUtil.getMenuInfo(menuList);
            List<MenuBean> allMenuInfo = MenuUtil.getMenuInfo(allMenu);
            
            if (CodeUtil.codeMap == null || CodeUtil.codeMap.size() == 0) {
                List<CodeEntity> codeList = service.getCodeList();
                CodeUtil.initCodeMap(codeList);
            }

            HttpSession session = ServletActionContext.getRequest()
                    .getSession();
            session.setAttribute(BaseConstant.SESSION_USER_INFO_KEY, bean);
            session.setAttribute(BaseConstant.SESSION_MENU_INFO_KEY, menuInfoList);
            session.setAttribute(BaseConstant.SESSION_ALL_MENU_INFO_KEY, allMenuInfo);

            map.put("user", bean);

            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String logout() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.removeAttribute(BaseConstant.SESSION_USER_INFO_KEY);

        return LOGIN;
    }
    
    public String execute() {
        return LOGIN;
    }
}
